package com.yl.chainStore.control.customer

import com.yl.chainStore.pojo.vo.customer.CustomerSelectVo
import com.yl.chainStore.pojo.vo.sick.CustomerSubscribeLogOperationVo
import com.yl.chainStore.pojo.vo.sick.SickMedicalHistorySelectVo
import com.yl.chainStore.pojo.vo.sick.UpdateSubscribeLogTimeVo
import com.yl.chainStore.services.*
import com.yl.chainStore.services.sick.ICustomerSubscribeLogService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.users.services.ICompanyDeptService
import com.yl.users.services.ICompanyUserService
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*
import java.text.SimpleDateFormat


/**
 * 电话机专用接口
 */
@RestController
@RequestMapping("phone/customer/info")
open class CustomerInfoByPhoneController(
	private val iWebCompanyService: IWebCompanyService,
	private val iCompanyDeptService: ICompanyDeptService,
	private val iCustomerInfoService: ICustomerInfoService,
	private val iCustomerSubscribeLogService: ICustomerSubscribeLogService
) {

	/**
	 * 来电弹屏
	 */
	@GetMapping("getCustomerByPhone/{phone}")
	@Transactional
	open fun getCustomerInfoByPhone(@PathVariable phone: String): ResponseBody {
		val resMap = mutableMapOf<String, Any?>()
		resMap["customerInfo"] = null
		resMap["subscribeLog"] = null
		resMap["historyLog"] = null
		val customerPage = iWebCompanyService.getCustomerPage(CustomerSelectVo(companyId = 501, phone = phone))
		if (customerPage.records.isEmpty()) {
			return returnSuccess(resMap)
		}
		val customerInfo = customerPage.records[0]
		resMap["customerInfo"] = customerInfo
		val lastRecordsDetail = iCustomerInfoService.getLastRecordsDetail(customerInfo.id)
		if (lastRecordsDetail?.masterUserName != null) {
			customerInfo.lastRecordDetailDoctorName = lastRecordsDetail.masterUserName
		}

		val latelySubscribeLog =
			iCustomerInfoService.getLatelySubscribeLog(customerId = customerInfo.id, customerInfo.companyId, null)

		resMap["subscribeLog"] = latelySubscribeLog
		if (latelySubscribeLog != null) {
			val dept = iCompanyDeptService.getDept(latelySubscribeLog.subCompanyId)
			latelySubscribeLog.subCompanyName = dept?.name
			if (latelySubscribeLog.itemIdsShow != null) {
				latelySubscribeLog.itemNames =
					iCustomerSubscribeLogService.getSubscribeItemsByIdsStr(latelySubscribeLog.itemIdsShow!!)
			}
		}


		resMap["historyLog"] = null
		val list = iCustomerInfoService.getMedicalHistoryList(SickMedicalHistorySelectVo(customerId = customerInfo.id))
		if (list.isNotEmpty()) {
			val historyLog = list[0]
			resMap["historyLog"] = historyLog
		}
		return returnSuccess(resMap)
	}

	/**
	 * 修改预约时间
	 */
	@PostMapping("updateSubscribeLogTime")
	@Transactional(rollbackFor = [Exception::class])
	open fun updateSubscribeLogTime(@RequestBody vo: UpdateSubscribeLogTimeVo): ResponseBody {
		val subLog = iCustomerSubscribeLogService.getById(vo.subscribeLogId) ?: return returnError(
			ErrorCode.DATA_IS_NOT_FIND,
			"未找到预约数据"
		)
		val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm")
		try {
			subLog.subscribeTime = dateFormat.parse(vo.timeString)

		}catch (e:Exception){
			return returnError(ErrorCode.PARAMETER_FORMAT_ERROR,"日期格式错误")
		}
		val updateInfo = iCustomerSubscribeLogService.updateInfo(subLog)
		if(updateInfo.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return updateInfo
		}
		return returnSuccess()
	}

	@PostMapping("cancelSubscribeLog/{id}")
	@Transactional(rollbackFor = [Exception::class])
	open fun cancelSubscribeLog(@PathVariable id:Long):ResponseBody{
		val cancelLog = iCustomerSubscribeLogService.cancelLog(CustomerSubscribeLogOperationVo(id))
		if(cancelLog.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return cancelLog
		}
		return returnSuccess()
	}

}
